﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class FlightSearchArchiveDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblFlightSearchArchive> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblFlightSearchArchives select a;
                return list.ToList<tblFlightSearchArchive>();
            }
            catch
            {
                return new List<tblFlightSearchArchive>();
            }
        }

        public tblFlightSearchArchive GetByKey(int id)
        {
            try
            {
                var obj = dbContext.tblFlightSearchArchives.SingleOrDefault(a => a.Id == id);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public int Insert(tblFlightSearchArchive obj)
        {
            try
            {
                dbContext.tblFlightSearchArchives.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                return obj.Id;
            }
            catch
            {
                return -1;
            }
        }

        public bool Update(tblFlightSearchArchive obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblFlightSearchArchives.SingleOrDefault(a => a.Id == obj.Id);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion

        public tblFlightSearchArchive GetExistedSearch(tblFlightSearchArchive input)
        {
            try
            {
                if (input.ItineraryType == true)
                {
                    var obj = dbContext.tblFlightSearchArchives.FirstOrDefault(a =>
                        a.ItineraryType == input.ItineraryType &&
                        a.DepartureAirportCode == input.DepartureAirportCode &&
                        a.DestinationAirportCode == input.DestinationAirportCode &&
                        a.DepartureDate == input.DepartureDate &&
                        a.ReturnDate == input.ReturnDate &&
                        a.Adult == input.Adult &&
                        a.Children == input.Children &&
                        a.Infant == input.Infant &&
                        a.Youth == input.Youth &&
                        a.Senior == input.Senior
                        );
                    return obj;
                }
                else
                {
                    var obj = dbContext.tblFlightSearchArchives.FirstOrDefault(a =>
                        a.ItineraryType == input.ItineraryType &&
                        a.DepartureAirportCode == input.DepartureAirportCode &&
                        a.DestinationAirportCode == input.DestinationAirportCode &&
                        a.DepartureDate == input.DepartureDate &&
                        a.Adult == input.Adult &&
                        a.Children == input.Children &&
                        a.Infant == input.Infant &&
                        a.Youth == input.Youth &&
                        a.Senior == input.Senior
                        );
                    return obj;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
